function [diversity_gain, fig_handle] = multiuser_diversity(SNR_dB, num_users, num_channels)
% 多用户分集分析
% 输入:
%   SNR_dB - 信噪比范围 (dB)
%   num_users - 用户数量数组
%   num_channels - 信道实现次数
% 输出:
%   diversity_gain - 分集增益矩阵
%   fig_handle - 图形句柄

    SNR_linear = 10.^(SNR_dB/10);
    diversity_gain = zeros(length(SNR_dB), length(num_users));
    
    % 添加路径
    addpath('../Common');
    colors = color_definitions();
    
    fprintf('=== 多用户分集分析 ===\n');
    
    for user_idx = 1:length(num_users)
        K = num_users(user_idx);
        fprintf('仿真 %d 用户分集增益...\n', K);
        
        for snr_idx = 1:length(SNR_dB)
            snr = SNR_linear(snr_idx);
            
            % 生成多个信道实现
            max_rates = zeros(1, num_channels);
            
            for chan_idx = 1:num_channels
                % 生成K个用户的信道
                user_channels = sqrt(0.5) * (randn(1, K) + 1i * randn(1, K));
                
                % 选择最佳用户 (最大载干比调度)
                [~, best_user] = max(abs(user_channels));
                best_channel = abs(user_channels(best_user))^2;
                
                % 计算该信道实现的速率
                sinr = snr * best_channel;
                max_rates(chan_idx) = log2(1 + sinr);
            end
            
            % 计算平均速率 (多用户分集增益)
            avg_rate = mean(max_rates);
            
            % 与单用户情况比较
            single_user_rate = log2(1 + snr);
            diversity_gain(snr_idx, user_idx) = avg_rate - single_user_rate;
        end
    end
    
    % 绘制结果
    fig_handle = figure('Name', '多用户分集分析', 'Position', [200, 200, 1000, 600]);
    
    subplot(2,3,1);
    for user_idx = 1:length(num_users)
        plot(SNR_dB, diversity_gain(:, user_idx), ...
             ['-', colors(user_idx)], 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('SNR (dB)');
    ylabel('分集增益 (bps/Hz)');
    title('多用户分集增益');
    legend(arrayfun(@(x) sprintf('%d用户', x), num_users, 'UniformOutput', false));
    
    % 分集增益与用户数量的关系
    snr_points = [5, 10, 15, 20]; % dB
    subplot(2,3,2);
    for snr_idx = 1:length(snr_points)
        snr_val = snr_points(snr_idx);
        snr_idx_in_data = find(SNR_dB == snr_val, 1);
        plot(num_users, diversity_gain(snr_idx_in_data, :), ...
             ['-', colors(snr_idx)], 'LineWidth', 2);
        hold on;
    end
    grid on;
    xlabel('用户数量');
    ylabel('分集增益 (bps/Hz)');
    title('分集增益 vs 用户数量');
    legend(arrayfun(@(x) sprintf('SNR=%d dB', x), snr_points, 'UniformOutput', false));
    
    % 信道分布分析
    subplot(2,3,3);
    K = 8;
    num_samples = 10000;
    user_channels = sqrt(0.5) * (randn(num_samples, K) + 1i * randn(num_samples, K));
    best_channels = max(abs(user_channels), [], 2);
    
    histogram(abs(user_channels(:, 1)), 50, 'Normalization', 'pdf');
    hold on;
    histogram(best_channels, 50, 'Normalization', 'pdf');
    grid on;
    xlabel('信道增益');
    ylabel('概率密度');
    title(sprintf('%d用户信道分布', K));
    legend('单用户', '最佳用户');
    
    % 调度公平性分析
    subplot(2,3,4);
    K = 8;
    num_slots = 1000;
    user_selections = zeros(1, K);
    
    for slot = 1:num_slots
        channels = sqrt(0.5) * (randn(1, K) + 1i * randn(1, K));
        [~, best_user] = max(abs(channels));
        user_selections(best_user) = user_selections(best_user) + 1;
    end
    
    bar(1:K, user_selections/num_slots);
    grid on;
    xlabel('用户索引');
    ylabel('选择概率');
    title('用户选择公平性');
    
    % 不同调度算法比较
    subplot(2,3,5);
    K = 8;
    snr = 15; % dB
    snr_linear = 10^(snr/10);
    
    % 轮询调度
    channels = sqrt(0.5) * (randn(1, K) + 1i * randn(1, K));
    round_robin_rate = mean(log2(1 + snr_linear * abs(channels).^2));
    
    % 最大载干比调度
    [~, best_user] = max(abs(channels));
    max_cir_rate = log2(1 + snr_linear * abs(channels(best_user))^2);
    
    % 比例公平调度 (简化)
    pf_rates = log2(1 + snr_linear * abs(channels).^2);
    avg_rates = mean(pf_rates) * ones(1, K);
    pf_metrics = pf_rates ./ avg_rates;
    [~, pf_user] = max(pf_metrics);
    pf_rate = log2(1 + snr_linear * abs(channels(pf_user))^2);
    
    algorithms = {'轮询', '最大CIR', '比例公平'};
    rates = [round_robin_rate, max_cir_rate, pf_rate];
    bar(1:3, rates);
    grid on;
    xlabel('调度算法');
    ylabel('速率 (bps/Hz)');
    title('调度算法性能比较');
    set(gca, 'XTick', 1:3, 'XTickLabel', algorithms);
    
    % 分集阶数分析
    subplot(2,3,6);
    K_range = 2:2:16;
    diversity_orders = zeros(1, length(K_range));
    
    for k_idx = 1:length(K_range)
        K = K_range(k_idx);
        % 通过不同SNR下的斜率估计分集阶数
        snr_low = 0:5:10;
        snr_high = 15:5:25;
        
        rates_low = zeros(1, length(snr_low));
        rates_high = zeros(1, length(snr_high));
        
        for snr_idx = 1:length(snr_low)
            snr = 10^(snr_low(snr_idx)/10);
            channels = sqrt(0.5) * (randn(1, K) + 1i * randn(1, K));
            [~, best_user] = max(abs(channels));
            rates_low(snr_idx) = log2(1 + snr * abs(channels(best_user))^2);
        end
        
        for snr_idx = 1:length(snr_high)
            snr = 10^(snr_high(snr_idx)/10);
            channels = sqrt(0.5) * (randn(1, K) + 1i * randn(1, K));
            [~, best_user] = max(abs(channels));
            rates_high(snr_idx) = log2(1 + snr * abs(channels(best_user))^2);
        end
        
        % 计算斜率变化 (简化分集阶数估计)
        slope_low = (rates_low(end) - rates_low(1)) / (snr_low(end) - snr_low(1));
        slope_high = (rates_high(end) - rates_high(1)) / (snr_high(end) - snr_high(1));
        diversity_orders(k_idx) = slope_high - slope_low;
    end
    
    plot(K_range, diversity_orders, 'o-', 'LineWidth', 2);
    grid on;
    xlabel('用户数量');
    ylabel('分集阶数');
    title('多用户分集阶数');
end